package com.zw.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWTHeader;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.ObjectBuffer;
import com.zw.commons.utlis.JwtHelper;
import com.zw.commons.utlis.SaveImage;
import com.zw.object.goods.GoodInfo;
import com.zw.goods.mapper.GoodInfoMapper;
import com.zw.goods.service.GoodInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.object.result.Result;
import com.zw.object.vo.GoodInfoTotalVo;
import com.zw.object.vo.GoodInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 物资信息表 服务实现类
 * </p>
 *
 * @author zw
 * @since 2023-03-15
 */
@Service
public class GoodInfoServiceImpl extends ServiceImpl<GoodInfoMapper, GoodInfo> implements GoodInfoService {

    private SaveImage saveImage = new SaveImage();
    @Override
    public Result goodInfoAll() {
        LambdaQueryWrapper<GoodInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(GoodInfo::getId, GoodInfo::getName, GoodInfo::getUnit, GoodInfo::getSize, GoodInfo::getTotal);
        List<GoodInfo> infoList = baseMapper.selectList(wrapper);
        return Result.ok(infoList);
    }

    @Override
    public Result goodInfoTotal(GoodInfoTotalVo vo) {
        LambdaQueryWrapper<GoodInfo> wrapper = new LambdaQueryWrapper<>();
        Page<GoodInfo> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        wrapper.like(StrUtil.isNotBlank(vo.getName()), GoodInfo::getName, vo.getName());
        wrapper.eq(vo.getTypeId() != null, GoodInfo::getTypeId, vo.getTypeId());
        wrapper.select(GoodInfo::getId, GoodInfo::getName, GoodInfo::getUnit, GoodInfo::getSize, GoodInfo::getTotal);
        Page<GoodInfo> infoPage = baseMapper.selectPage(page, wrapper);
        return Result.ok(infoPage);
    }

    @Override
    public Result list(GoodInfoVo vo) {
        LambdaQueryWrapper<GoodInfo> wrapper = new LambdaQueryWrapper<>();
        Page<GoodInfo> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        wrapper.like(StrUtil.isNotBlank(vo.getName()), GoodInfo::getName, vo.getName());
        wrapper.like(StrUtil.isNotBlank(vo.getCreateBy()), GoodInfo::getCreateBy, vo.getCreateBy());
        wrapper.eq(vo.getStatus() != null, GoodInfo::getStatus, vo.getStatus());
        wrapper.eq(vo.getTypeId() != null, GoodInfo::getTypeId, vo.getTypeId());
        Page<GoodInfo> infoPage = baseMapper.selectPage(page, wrapper);
        return Result.ok(infoPage);
    }

    @Override
    public Result save(MultipartFile file, String json, HttpServletRequest request) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        GoodInfo goodInfo = mapper.readValue(json, GoodInfo.class);
        if (!file.isEmpty()) {
            String saveImageUrl = saveImage.saveImage(file);
            if(saveImageUrl == null){
                return  Result.fail("图片上传失败");
            }
            goodInfo.setImg(saveImageUrl);
        }
        goodInfo.setCreateBy(JwtHelper.getUsername(request.getHeader("token")));
        int insert = baseMapper.insert(goodInfo);
        return insert>0?Result.ok():Result.fail();
    }

    @Override
    public Result update(MultipartFile file, String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        GoodInfo goodInfo = mapper.readValue(json, GoodInfo.class);
        if (file != null && !file.isEmpty()) {
            String imageUrl = saveImage.saveImage(file);
            if (imageUrl == null) {
                return Result.fail("图片上传失败");
            }
            goodInfo.setImg(imageUrl);
        }
        int update = baseMapper.updateById(goodInfo);
        return update>0?Result.ok():Result.fail();
    }

    @Override
    public Result delete(Long[] ids) {
        int batchIds = baseMapper.deleteBatchIds(Arrays.asList(ids));
        return batchIds>0?Result.ok():Result.fail();
    }
}
